package com.sunnybear.framework.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.sunnybear.framework.R;

import java.util.ArrayList;
import java.util.List;

/**
 * loading加载框
 *  2018/4/11.
 */
public class LoadingDialog extends Dialog {
    private TextView msgText;
    private String message;
    private boolean isShowMessage = true;
    private boolean isCancelable = false;
    private boolean isCancelOutside = false;
    private List<String> texts = new ArrayList<>();


    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.dialog_loading, null);
        TextView msgText = view.findViewById(R.id.tipTextView);
        if (isShowMessage)
            msgText.setText(message);
        else
            msgText.setVisibility(View.GONE);
        texts.add(message);
        setContentView(view);
        setCancelable(isCancelable);
        setCanceledOnTouchOutside(isCancelOutside);
    }

    /**
     * 获取Builder对象
     *
     * @param context
     * @return
     */
    public static Builder getBuilder(Context context) {
        return new Builder(context);
    }

    public LoadingDialog message(String message) {
        this.message = message;
        return this;
    }

    public LoadingDialog isShowMessage(boolean isShowMessage) {
        this.isShowMessage = isShowMessage;
        return this;
    }

    public LoadingDialog isCancelable(boolean isCancelable) {
        this.isCancelable = isCancelable;
        return this;
    }

    public LoadingDialog isCancelOutside(boolean isCancelOutside) {
        this.isCancelOutside = isCancelOutside;
        return this;
    }

    public TextView getMsgText() {
        return msgText;
    }

    public String getMessage() {
        return message;
    }

    public boolean isShowMessage() {
        return isShowMessage;
    }

    public boolean isCancelable() {
        return isCancelable;
    }

    public boolean isCancelOutside() {
        return isCancelOutside;
    }

    public List<String> getTexts() {
        return texts;
    }

    public static class Builder {

        private Context context;
        private String message;
        private boolean isShowMessage = true;
        private boolean isCancelable = false;
        private boolean isCancelOutside = false;

        private Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置提示信息
         *
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * 设置是否显示提示信息
         *
         * @param isShowMessage
         * @return
         */
        public Builder setShowMessage(boolean isShowMessage) {
            this.isShowMessage = isShowMessage;
            return this;
        }

        /**
         * 设置是否可以按返回键取消
         *
         * @param isCancelable
         * @return
         */
        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        /**
         * 设置是否可以取消
         *
         * @param isCancelOutside
         * @return
         */
        public Builder setCancelOutside(boolean isCancelOutside) {
            this.isCancelOutside = isCancelOutside;
            return this;
        }

        public LoadingDialog create() {
            LoadingDialog loadingDialog = new LoadingDialog(context, R.style.LoadingDialogStyle);
            loadingDialog.message(message).isCancelable(isCancelable)
                    .isCancelOutside(isCancelOutside).isShowMessage(isShowMessage).init();
            return loadingDialog;
        }
    }

    @Override
    public void dismiss() {
        if (texts.size() <= 1) {
            super.dismiss();
        } else {
            texts.remove(0);
            msgText.setText(texts.get(0));
        }
    }
}
